﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevEx.Thanh.Class ;

namespace DevEx.Thanh.UserControl
{
    public partial class frmLogin : DevExpress.XtraEditors.XtraForm
    {
        private CUserController ctrlUser = new CUserController();

        public frmLogin()
        {
            InitializeComponent();
        }

        private void frmLogin_Load(object sender, EventArgs e)
        {
            lblPasswordError.Visible = false;
            lblUserNameError.Visible = false;
            GlobalVar.LoginOK = false;
            GlobalVar.CurrentUser = null;
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            //kiểm tra
            if (String.IsNullOrEmpty(txtUsername.Text))
            {
                lblUserNameError.Visible = true;
                return;
            }
            if (String.IsNullOrEmpty(txtPassword.Text))
            {
                lblPasswordError.Visible = true;
                return;
            }
            //kiểm tra username pass
            if (ctrlUser.CheckUser(txtUsername.Text, txtPassword.Text))
            {
                GlobalVar.CurrentUser = ctrlUser.SelectUserByUserName(txtUsername.Text);
                GlobalVar.LoginOK = true;
                this.Close();
            }
            else
            {
                GlobalVar.LoginOK = false;
                GlobalVar.CurrentUser = null;
                if (MessageBox.Show( "Tên đăng nhập hoặc mật khẩu không đúng. Đăng nhập lại?","Thông báo", MessageBoxButtons.OKCancel )==DialogResult.OK )
                {
                    lblPasswordError.Visible = false;
                    lblUserNameError.Visible = false;
                    txtUsername.EditValue = "";
                    txtPassword.EditValue = "";
                    txtUsername.Focus();
                }
                else
                {
                    this.Close();
                }
            }
        }

        private void btHuy_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}